package com.szj.pet.hospital.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.szj.pet.hospital.common.result.Result;
import com.szj.pet.hospital.model.hosp.Staff;
import com.szj.pet.hospital.service.StaffService;
import com.szj.pet.hospital.vo.hosp.StaffQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.util.List;

@Api(tags = "员工管理")
@RestController
@RequestMapping("admin/hosp/staff")
//@CrossOrigin
public class StaffController {
    @Autowired
    private StaffService staffService;

    //1 查询员工信息所有信息
    @ApiOperation(value = "获取所有员工信息")
    @GetMapping("findAll")
    public Result findAllStaff() {
        //调用service的方法
        List<Staff> list = staffService.list();
        return Result.ok(list);
    }

    //2 逻辑删除员工信息
    @ApiOperation(value = "逻辑删除员工信息")
    @DeleteMapping("{id}")
    public Result removeStaff(@PathVariable Long id) {
        boolean flag = staffService.removeById(id);
        if (flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    //3 条件查询带分页
    @ApiOperation(value = "员工信息条件查询带分页")
    @PostMapping("findPageStaff/{current}/{limit}")
    public Result findPageStaff(@PathVariable long current,
                                  @PathVariable long limit,
                                  @RequestBody(required = false) StaffQueryVo staffQueryVo) {
        //创建page对象，传递当前页，每页记录数
        Page<Staff> page = new Page<>(current, limit);
        //构建条件
        QueryWrapper<Staff> wrapper = new QueryWrapper<>();
        String name = staffQueryVo.getName();//名字
        String phone = staffQueryVo.getPhone();//手机号码

        if (!StringUtils.isEmpty(name)) {
            wrapper.like("name", staffQueryVo.getName());
        }
        if (!StringUtils.isEmpty(phone)) {
            wrapper.like("phone", staffQueryVo.getPhone());
        }

        //调用方法实现分页查询
        IPage<Staff> pageStaff = staffService.page(page, wrapper);

        //返回结果
        return Result.ok(pageStaff);
    }

    //4 添加员工信息
    @PostMapping("saveStaff")
    public Result saveStaff(@RequestBody Staff staff) throws ParseException {

        //调用service
        boolean save = staffService.save(staff);
        if (save) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    //5 根据id获取员工信息
    @GetMapping("getStaff/{id}")
    public Result getStaff(@PathVariable Long id) {

        Staff staff = staffService.getById(id);
        return Result.ok(staff);
    }
    //6 修改员工信息
    @PostMapping("updateStaff")
    public Result updateStaff(@RequestBody Staff staff) {
        boolean flag = staffService.updateById(staff);
        if (flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    //7 批量删除员工信息
    @DeleteMapping("batchRemove")
    public Result batchRemoveStaff(@RequestBody List<Long> idList) {
        staffService.removeByIds(idList);
        return Result.ok();
    }

    //导出数据字典接口
    @GetMapping("exportData")
    public void exportDict(HttpServletResponse response) {
        staffService.exportStaffData(response);
    }

    //8 预约信息处理和未处理
    /*@PutMapping("lockReserve/{id}/{status}")
    public Result lockHospitalSet(@PathVariable Long id,
                                  @PathVariable Integer status) {
        //根据id查询医院设置信息
        Staff staff = staffService.getById(id);= staffService.getById(id);
        //设置状态
        reserve.setStatus(status);
        //调用方法
        reserveService.updateById(reserve);
        return Result.ok();
    }*/
}
